package com.sales.service.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sales.service.R;
import com.sales.service.data.transaction.TaskTransaction;
import com.sales.service.data.transaction.containers.TaskContainer;
import com.sales.service.data.transaction.notifier.UINotifier;
import com.sales.service.thread.ServiceThreadPool;

/**
 * This is the Task activity.
 * the task request is initiated here and the thread is started.
 * the response is obtained and showed in as the list in listview.
 * Progress dialog is used.
 *  
 * @author Sega
 *
 */
public class TaskActivity extends BaseActivity implements UINotifier, OnItemClickListener {

	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "Task ::";
	
	private ListView taskListView;
	private TaskArrayAdaptor adap=null;
	private ArrayList<Object> arrayTaskResult;

	/* (non-Javadoc)
	 * @see com.sales.service.view.BaseActivity#onCreate(android.os.Bundle)
	 * 
	 * the oncreate overridden method.
	 * the layout is set.
	 * the header content is set.
	 * the request is initiated.
	 * thread instance is created to start the thread.
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.task_layout);
		Log.i(TagProject, TagPage);

		showDialog(DIALOG_ID_PROGRESS);

		setHeaderContent();

		TaskTransaction taskTrnc = new TaskTransaction(this);
		ServiceThreadPool.getInstance();

	}

	/**
	 * Header content is set.
	 */
	private void setHeaderContent() {
		Button btnLeft = (Button) findViewById(R.id.btnLeft);
		btnLeft.setVisibility(View.INVISIBLE);

		Button btnRight = (Button) findViewById(R.id.btnRight);
		btnRight.setVisibility(View.INVISIBLE);

		TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
		txtTitle.setText("Task");
	}

	/* (non-Javadoc)
	 * @see com.sales.service.data.transaction.notifier.UINotifier#onSuccess(java.util.ArrayList)
	 * 
	 * method to display the obtained result in the list view.
	 *  
	 */
	@Override
	public void onSuccess(ArrayList<Object> results) {

		this.arrayTaskResult = results;

		taskListView = (ListView) findViewById(R.id.listvwTaskActivity);
		adap = new TaskArrayAdaptor(this, 0, arrayTaskResult);
		taskListView.setAdapter(adap);
		taskListView.setOnItemClickListener(this);

		Log.i(TagProject, TagPage + "Helllo Updated");
		hideProgress();
	}

	/* (non-Javadoc)
	 * @see com.sales.service.data.transaction.notifier.UINotifier#onError()
	 * 
	 * method to display failure result from thread.
	 */
	@Override
	public void onError() {
		Log.i(TagProject, TagPage + "NO DISPLAY");
		hideProgress();
	}
	
	/**
	 * class to set adaptor for the listview.
	 * array adaptor is used.
	 * 
	 * @author Sega
	 *
	 */
	private class TaskArrayAdaptor extends ArrayAdapter<Object>{

		Context adContext;
		
		/**
		 * constructor of the array adaptor class
		 * 
		 * @param context
		 * @param textViewResourceId
		 * @param arrayTaskResult
		 */
		public TaskArrayAdaptor(Context context, int textViewResourceId,ArrayList<Object> arrayTaskResult) {
			super(context, textViewResourceId);
			this.adContext = context;
		}

		/* (non-Javadoc)
		 * @see android.widget.ArrayAdapter#getCount()
		 * 
		 * array size is obtained.
		 */
		@Override
		public int getCount() {
			return arrayTaskResult.size();
		}
		
		/* (non-Javadoc)
		 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
		 * 
		 * the listview layout is inflated in the getview method.
		 * 
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) adContext.getSystemService(adContext.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.task_listview_layout, null);
			TextView txtComp = (TextView) rowview.findViewById(R.id.txtSubject);
			TextView txtName = (TextView) rowview.findViewById(R.id.txtDueDate);
			txtComp.setText(((TaskContainer) arrayTaskResult.get(position)).getSubject());
			txtName.setText(((TaskContainer) arrayTaskResult.get(position)).getDueDate());
			return rowview;
		}
	}

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 * 
	 * The itemclick event to call a new activity to display the contents.
	 * 
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int itemPosition, long arg3) {
		Log.i(TagProject, TagPage + "selected item: " + ((TaskContainer) arrayTaskResult.get(itemPosition)).getSubject());
		Intent in = new Intent(this, TaskInfoActivity.class);
		in.putExtra("1", ((TaskContainer) arrayTaskResult.get(itemPosition)).getSubject());
		startActivity(in);
	}
}